package com.chenzhe.apipost;

import com.chenzhe.apipost.util.RSAUtils;

public class RSATester {

    public static String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKgbeJOnKbicVLQIc/0H+jkoWr3Yig0woEyxTSC0eTgRSmzu1/zFgng15mvzascV50H/clqYWlUbaW2+8LjhYCMWd2GHs4MoXaoNAVUL5Y2tXK8wyfsKs8fJNmDFsMvkVsARlK4ZOF+9u8GaE5f/qZemiVBL4GDU4C4CG7XDxV/NAgMBAAECgYEAga63hMg28jh/S2cd+zu1N3S/E6dBUE2Ly74XLryqqqsEDCbwrgm7zXpfKDXe0HhAL3SdRfAz92WvHj9nOmh6nTAeWJQsctiXMcFJ52Es3VzyTwZSr/0yaWUNUC8fvPwXh80LFiwULxfXBnqoB2LXxAxhpHAXUS/TYnkS1qCXGuECQQD+JYI5XSw6JL1sQ9E/Ur6trRxkcBJSLRyHb8qnzfFva8RF6FfswxRdeggOB/PRDdMjMLGgd9yVZWrpS3wfl2l1AkEAqVVTwstvXgQhK5sf5ix7FfzQSEDxqJvxdMr6+zS8c2LW3nWt2p5dRpLXh7qNggkSJGpfqNo2i+LgzsmDKZn5+QJBALz7bUt0sjeGLGj+QPHCeNGywUMaZzU+IFHB2kNPi7Hbw/Zr4D3JjjI4VRcB6NbSXed0BRGbL6bT5F0YaXMgZyUCQGvSOy1z4AvbUUu00ND9Tbqoae83+yhDc7HMdZdy8vP94ZdxEyx7XiVL5xUzwlskb3rkiNi5abYU1tSVF4Fr2/ECQQCXoLmiNR81L1C24OgzWStgc/nX5eZUj2FTTXtRpheYGHHQ3ifSusP912OW7NdLQ6X4LJ7nLUcUcUZqycpTMPyM";
    public  static String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCoG3iTpym4nFS0CHP9B/o5KFq92IoNMKBMsU0gtHk4EUps7tf8xYJ4NeZr82rHFedB/3JamFpVG2ltvvC44WAjFndhh7ODKF2qDQFVC+WNrVyvMMn7CrPHyTZgxbDL5FbAEZSuGThfvbvBmhOX/6mXpolQS+Bg1OAuAhu1w8VfzQIDAQAB";

//    static {
//        try {
//            Map<String, Object> keyMap = RSAUtils.genKeyPair();
//            publicKey = RSAUtils.getPublicKey(keyMap);
//            privateKey = RSAUtils.getPrivateKey(keyMap);
//            System.err.println("公钥: \n\r" + publicKey);
//            System.err.println("私钥： \n\r" + privateKey);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    
    public static void main(String[] args) throws Exception {
        //test();
        testSign();
    }

    static void test() throws Exception {
        System.err.println("公钥加密——私钥解密");
        String source = "{\"age\":12,\"name\":\"小明\",\"phone\":\"1333333333\"}";
        System.out.println("\r加密前文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
        System.out.println("加密后文字：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
        String target = new String(decodedData);
        System.out.println("解密后文字: \r\n" + target);
    }

    static void testSign() throws Exception {
        System.err.println("私钥加密——公钥解密");
        String source = "{\"age\":12,\"name\":\"小明\",\"phone\":\"1333333333\"}";
        System.out.println("原文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        System.out.println("加密后：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        System.out.println("解密后: \r\n" + target);
        System.err.println("私钥签名——公钥验证签名");
        String sign = RSAUtils.sign(encodedData, privateKey);
        System.err.println("签名:\r" + sign);
        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
        System.err.println("验证结果:\r" + status);
    }
}
